package com.huajx.tuopuda.client.udp;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-09-25 09:50
 */
public class TuopudaUdpClient3 extends JFrame implements Runnable {
    private JTextArea jt = new JTextArea(15, 20);
    private PrintWriter writer;
    private ServerSocket server;
    private int port;
    private InetAddress iaddress = null;
    private MulticastSocket socket = null;
    private boolean flag = true;
    Thread thread;
    JButton jb_s = new JButton("开始接收");
    JButton jb_e = new JButton("停止接收");

    public TuopudaUdpClient3() {
        init();
        thread = new Thread(this);
        jb_s.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                jb_s.setBackground(Color.GRAY);
                jb_e.setBackground(Color.RED);
                port = 60000;
                try {
                    iaddress = InetAddress.getByName("224.0.0.3");
                    socket = new MulticastSocket(port);
                    socket.joinGroup(iaddress);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

                thread.start();
            }
        });

        jb_e.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                flag = false;
                jb_s.setBackground(Color.GREEN);
                jb_e.setBackground(Color.GRAY);
            }
        });
    }

    @Override
    public void run() {
        jt.append("开始接收数据！！\n");
        while (true) {

            if (!flag) {
                break;
            }

            byte data[] = new byte[1024];
            DatagramPacket packet = null;
            packet = new DatagramPacket(data, data.length, iaddress, port);
            try {
                socket.receive(packet);
                String msg = new String(data);
                jt.append(msg + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void init() {
        setTitle("翼校云平安学子门禁考勤控制台");
        setLocationRelativeTo(null);
        Container con = getContentPane();
        JScrollPane js = new JScrollPane(jt);

        JPanel jp = new JPanel();
        jp.setLayout(new FlowLayout());

        jb_s.setBackground(Color.GREEN);
        jb_e.setBackground(Color.RED);
        jp.add(jb_s);
        jp.add(jb_e);
        con.add(js, "North");
        con.add(jp, "South");
        setVisible(true);
        setSize(800, 600);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] agrs) {
        new TuopudaUdpClient3();
    }

}